/*
 * @Author: funlee 
 * @Email: i@funlee.cn 
 * @Date: 2017-11-21 10:51:01 
 * @Last Modified time:   2017-11-21 10:51:01 
 * @Description: promise与异步编程 
 */
let promise = readFile('test.txt');
promise.then(function(contents) {
    console.log(contents)
  }, function(err) {
    console.error(err.message)
  })
  /**
   * then()方法和catch()方法一起使用才能更好滴处理异步编程结果
   */
promise.catch(function(err) {
  console.error(err.message)
})
promise.then(null, function(err) {
    console.error(err.message)
  })
  /**
   * Promise执行器会立即执行，然后才执行后续流程中的代码
   */
let promise = new Promise(function(resolve, reject) {
  console.log('Promise');
  resolve()
})
console.log('Hi');
//Promise
//Hi
/**
 * 调用resolve()后会触发一个异步操作，传入then()和catch()方法的函数会被添加到任务队列中并异步执行
 */
let promise = new Promise(function(resolve, reject) {
  console.log('Promise');
  resolve()
})
promise.then(function() {
  console.log('resolved')
})
console.log('hi')
  //Promise
  //hi
  //resolved
  /**
   * resolve()和reject()
   */
let promise = Promise.resolve(43)
promise.then(function(value) {
  console.log(value) //43
})s